import { Component, OnInit } from '@angular/core'
import { JsonApiService } from 'app/core/api/json-api.service' //http请求配置引入
import { Router } from '@angular/router' //路由引入

declare var $: any
@Component({
  selector: 'fault-type-configuration',
  templateUrl: './fault-type-configuration.component.html',
  styleUrls: ['./fault-type-configuration.css']
})
export class FaultTypeConfigurationComponent implements OnInit {
  // initData: any = {
  //   id: '',
  //   typeName: '',
  //   level: 2,
  // }
  addRegion: any = {
    level: 2,
    typeName: '',
    type: '',
    parentId: ''
  }
  cityId: string = ''
  provinceId: string = ''
  levelOption: any = [
    {
      text: '[一级]故障类型',
      value: 2
    },
    {
      text: '[二级]故障类型',
      value: 3
    },
    {
      text: '[三级]故障类型',
      value: 4
    }
  ]
  provinceList: any = []
  cityList: any = []
  countyList: any = []
  provinceSelected: string = '' // 一级表格选中的id
  citySelected: string = '' // 二级表格选中的id
  countySelected: string = '' // 三级表格选中的id
  provinceKey: string = '' // 输入的一级搜索
  cityKey: string = '' // 输入的二级搜索
  countyKey: string = '' // 输入的三级搜索
  twoAreaName: string = ''
  threeAreaName: string = ''
  editState: boolean = false // 编辑状态
  editId: string = ''
  constructor(private jsonApiService: JsonApiService, private router: Router) {}
  ngOnInit() {
    this.load(2)
  }
  //table刷新
  refresh() {
    this.load(2)
  }
  //初始化加载list数据  TODO
  load(level, id: number | string = 0) {
    let levelCopy = 1
    if (level === 3) {
      levelCopy = 2
    } else if (level == 4) {
      levelCopy = 3
    }
    let data = {
      level: levelCopy,
      id: id
    }
    this.jsonApiService.http_obj('sanitationFaultType/list.html', data).subscribe(result => {
      if (level == 3) {
        this.cityList = result.data || []
        this.citySelected = ''
        this.countyList = []
      } else if (level == 4) {
        this.countyList = result.data || []
        this.countySelected = ''
      } else {
        this.provinceList = result.data || []
        this.cityList = []
        this.countyList = []
        this.provinceSelected = ''
        this.citySelected = ''
        this.countySelected = ''
      }
    })
  }
  // 添加
  addShowModal(modal) {
    this.editState = false
    for (let key in this.addRegion) {
      this.addRegion[key] = ''
    }
    this.addRegion.level = 3
    this.provinceId = ''
    this.cityId = ''
    modal.show()
  }
  // 选择层级
  changeLevel(val) {}
  // 选择行政区划一级
  changeProvinceOption(val) {
    if (val) {
      this.cityId = ''
      this.load(3, val)
    }
  }
  changeCityOption(val) {
    if (val) {
      this.load(4, val)
    }
  }
  addRegionClose(modal) {
    modal.hide()
  }
  // 提交添加
  addRegionSubmit(modal) {
    if (this.addRegion.level == 3) {
      if (this.provinceId) {
        this.addRegion.parentId = this.provinceId
      } else {
        this.jsonApiService.message('请选择上级故障类型[一级]')
        return
      }
    } else if (this.addRegion.level == 4) {
      if (this.cityId) {
        this.addRegion.parentId = this.cityId
      } else {
        this.jsonApiService.message(this.provinceId ? '请选择上级故障类型[二级]' : '请选择上级故障类型[一级]')
        return
      }
    }
    if (!this.addRegion.type) {
      this.jsonApiService.message('请填写故障编号')
      return
    }
    if (!this.addRegion.typeName) {
      this.jsonApiService.message('请填写故障类型名称')
      return
    }
    this.addRegion.type = this.addRegion.type ? String(this.addRegion.type) : ''
    if (this.editState) {
      this.addRegion.id = this.editId
    }
    this.jsonApiService.http_obj('sanitationFaultType/add.html', this.addRegion).subscribe(result => {
      this.jsonApiService.message(result.message)
      if (this.addRegion.level == 2) {
        this.load(this.addRegion.level)
      } else if (this.addRegion.level == 3) {
        this.load(this.addRegion.level, this.provinceId)
      } else {
        this.load(this.addRegion.level, this.cityId)
      }
      modal.hide()
    })
  }
  // 删除
  regionDelete(level) {
    let curId = ''
    if (level == 2) {
      curId = this.provinceSelected
    } else if (level == 3) {
      curId = this.citySelected
    } else {
      curId = this.countySelected
    }
    let text = '此操作将永久删除数据,确认要删除?'
    this.jsonApiService.confirm_message(text).then(() => {
      this.jsonApiService.http_obj('sanitationFaultType/delete.html', { id: curId }).subscribe(result => {
        if (level == 2) {
          this.load(level)
        } else if (level == 3) {
          this.load(level, this.provinceSelected)
        } else {
          this.load(level, this.citySelected)
        }
        this.jsonApiService.message(result.message)
      })
    })
  }
  // 编辑
  regionEdit(id, modal) {
    this.editState = true
    this.editId = id
    this.jsonApiService.http_obj('region/listById', { id: id }).subscribe(result => {
      if (result.data && result.data.length) {
        let editArr = result.data.filter(item => item.id == id)
        this.addRegion.level = editArr[0].level
        this.addRegion.typeName = editArr[0].typeName
        this.addRegion.type = editArr[0].type
        if (editArr[0].level == 3) {
          let editParent = result.data.filter(item => item.id == editArr[0].parentId)
          this.twoAreaName = editParent[0].typeName
          this.provinceId = editParent[0].id
        } else if (editArr[0].level == 4) {
          let editParent = result.data.filter(item => item.id == editArr[0].parentId)
          let editParentLord = result.data.filter(item => item.id == editParent[0].parentId)
          this.twoAreaName = editParentLord[0].typeName
          this.threeAreaName = editParent[0].typeName
          this.cityId = editParent[0].id
          this.provinceId = editParentLord[0].id
        }
        modal.show()
      }
    })
  }
  // 选中一级行政区
  rowSelectEvent(row, level) {
    if (level == 3) {
      this.provinceSelected = row.id
      this.citySelected = ''
      this.countySelected = ''
      this.countyList = []
      this.load(level, row.id)
    } else if (level == 4) {
      this.citySelected = row.id
      this.countySelected = ''
      // this.load(level, row.id)
    } else {
      this.countySelected = row.id
    }
  }
  // 搜索
  tableSearch(level) {
    let id = '0'
    let typeName = ''
    let levelCopy = 1
    if (level == 2) {
      typeName = this.provinceKey
      levelCopy = 1
    } else if (level == 3) {
      typeName = this.cityKey
      levelCopy = 2
      if (this.provinceSelected) id = this.provinceSelected
    } else {
      typeName = this.countyKey
      levelCopy = 3
      if (this.citySelected) id = this.citySelected
    }
    let data = {
      level: levelCopy,
      typeName: typeName,
      id
    }
    this.jsonApiService.http_obj('sanitationFaultType/list.html', data).subscribe(result => {
      if (level == 2) {
        this.provinceList = result.data || []
        this.cityList = []
        this.countyList = []
        this.provinceSelected = ''
        this.citySelected = ''
        this.countySelected = ''
      } else if (level == 3) {
        this.cityList = result.data || []
        this.citySelected = ''
        this.countyList = []
      } else {
        this.countyList = result.data || []
        this.countySelected = ''
      }
    })
  }
  keyupAreaNumber(e) {
    const invalidChars = ['-', '+', 'e', '.', 'E']
    if (invalidChars.indexOf(e.key) !== -1) {
      e.preventDefault()
    }
  }
}
